package com.bibrApp.difu.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bibrApp.difu.R;

import java.text.DecimalFormat;

public class ExtraEditView extends LinearLayout {

    EditText editText;
    TextView tvAdd;
    TextView tvSub;
    String extra = "";//单位
    double count = 0;//每次点击变化的幅度
    String decimal = "";//格式化
    private String etStr;//et输入的文字
    private OnClickAddOrReduceListener onClickAddOrReduceListener;

    public ExtraEditView(Context context) {
        super(context);
        init();
    }

    public ExtraEditView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ExtraEditView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public OnClickAddOrReduceListener getOnClickAddOrReduceListener() {
        return onClickAddOrReduceListener;
    }

    public void setOnClickAddOrReduceListener(OnClickAddOrReduceListener onClickAddOrReduceListener) {
        this.onClickAddOrReduceListener = onClickAddOrReduceListener;
    }

    private void init() {

        View view = inflate(getContext(), R.layout.view_extra, null);


        editText = view.findViewById(R.id.edittext);
        tvSub = view.findViewById(R.id.tv_sub);
        tvAdd = view.findViewById(R.id.tv_add);

        tvSub.setOnClickListener(view1 -> {
            sub();
            if(onClickAddOrReduceListener != null){
                onClickAddOrReduceListener.onclickAR();
            }
        });
        tvAdd.setOnClickListener(view1 -> {
            add();
            if(onClickAddOrReduceListener != null){
                onClickAddOrReduceListener.onclickAR();
            }
        });


        addView(view);
    }

    private void add() {
        String str = editText.getText().toString();
        if (TextUtils.isEmpty(str)) {
            value = 0 + count;
        } else if (!TextUtils.isEmpty(extra) && TextUtils.isEmpty(str.replace(extra, ""))) {
            value = 0 + count;
        } else {
            try {
                value = Double.parseDouble(str.replace(extra, ""));
                value += count;
            } catch (Exception e) {
                value = 0 + count;
            }
        }

        DecimalFormat df1 = new DecimalFormat(decimal);
        editText.setText(df1.format(value) + extra);
    }
    double value = 0;
    private void sub() {

        String str = editText.getText().toString();
        if (TextUtils.isEmpty(str)) {
            value = 0 - count;
        } else if (!TextUtils.isEmpty(extra) && TextUtils.isEmpty(str.replace(extra, ""))) {
            value = 0 - count;
        } else {
            try {
                value = Double.parseDouble(str.replace(extra, ""));
                value -= count;
            } catch (Exception e) {
                value = 0 - count;
            }
        }
        if (value < 0) {
            value = 0;
        }
        if(value > 100){
            value = 100;
        }

        DecimalFormat df1 = new DecimalFormat(decimal);

        editText.setText(df1.format(value) + extra);
    }


    public void setText(String text) {
        editText.setText(text);
    }

    public String getExtra() {
        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }

    public double getCount() {
        return count;
    }

    public void setCount(double count) {
        this.count = count;
    }

    public String getDecimal() {
        return decimal;
    }

    public void setDecimal(String decimal) {
        this.decimal = decimal;
    }
    public String getEtStr() {
        Log.d("dj","====value==="+value);
        Log.d("dj","====value==="+value/100);
        return String.valueOf(value/100);
    }

    public void setEtStr(double etStr) {
        this.value = etStr;
    }

    public interface OnClickAddOrReduceListener{
        void onclickAR();
    }
}
